<?php 
session_start();
require_once ("class/m_category.php");

	$m = new M_category();
	
	if (isset($_POST['m'])  &&  $_POST['m']!=""){
		if ($_POST['m']== 'detail'){
			if(isset($_POST['id']) && $_POST['id']!=""){ 
				$id=$_POST['id'];
				//$id=1;
				echo json_encode( $m->getDataDetail($id) );
			}else{
				echo "Errro";
			}
		}else if ($_POST['m']== 'add'){
			$ar= $_POST['var'];
			$ar["CreatedBy"]=$_SESSION['username'];
			$ar["CreatedDate"]=date( "Y/m/d H:i:s" );
			
			if ($m->checkCategory($ar["CatName"])=="0"){
				echo "error";
			}else{
				
				$id=$m->InsertData($ar);
				$status="";
				if($ar["Status"]==1)
					$status="Actived";
				else
					$status="Disabled";
				
				$ar["Status"]=$status;
				$ar["CatID"]=$id;
				
				echo "<tr>".genTable($ar, $_POST['rows']+1)."</tr>";
			}
			
		}else if ($_POST['m']=='edit'){
			if (isset($_POST['var'])){
				$ar=$_POST['var'];
				$flag=isset($ar);

				$ar["UpdatedBy"]=$_SESSION['username'];
				$ar["UpdatedDate"]=date( "Y/m/d H:i:s" );
				
				$sqlScript="select * from tb_product_category where IsDeleted=0 and CatID='". $_POST['id'] ."'";
				$m->updateData($sqlScript, $ar);
				
				$status="";
				if($ar["Status"]==1)
					$status="Actived";
				else
					$status="Disabled";
				
				$ar["Status"]=$status;
				$ar["CatID"]=$_POST['id'];
				echo genTable($ar,$_POST['rows']);
			}
					
		}else if ($_POST['m']=='del'){
			if(isset($_POST['var'])){
				$o=$_POST['var'];
				$m->delData($o["id"]);
			}
		}else if ($_POST['m']== 'search'){
			$name=$_POST['name'];
			$page=$_POST['page'];
			$page=isset($page)?$page:1;	
			$result=$m->getData($name,$page);
			$rows=$m->getRowCount();
			$i=1 + (($page-1) * $m->getListPage());
			$html="";
			if ($rows > 0){
				while($row = $result->FetchRow()){
					If($i%2 <> 0){
						$html .= "<tr class=row1>";
					} else{
						$html .= "<tr>";
					}
					$html .= genTable($row,$i);
					$html .= "</tr>";
					$i++;
				}
			}else{
				$html="<tr>"
					."<td colspan='5' align='center' ><img src='./images/info.gif'/><b/></td>"
					."</tr>";
			}
			
			//Return result , rows , page
			$ar_result=array('data'=>$html,
							'rows'=>$rows,
							'totalpage'=>$m->getTotalPage());
			echo json_encode( $ar_result );
 
		}
	}else{echo"Error2";}

	function genTable($row,$i){
		global $categoryScreen;
			$s.="<td align='center'>".$i."</td>".
				"<td style='display:none'>".$row["CatID"]."</td>" .
				"<td align='center'>".$row["CatName"]."</td>" .
				"<td align='center'>".$row["Status"]."</td>";
				
			if ($_SESSION["permission"][$categoryScreen]['Edit']=='1') 							
				$s.="<td align='center'><img src='./images/edit.png' class='btnEdit' style='cursor:pointer' title='Edit' border='0'/></td>";
			else 
				$s.="<td>&nbsp;</td>";
				
			if ($_SESSION["permission"][$categoryScreen]['Deleted']=='1') 					
				$s.="<td align='center'><img src='./images/remove.png' class='btnDel' style='cursor:pointer' title='Delete' border='0'/></td>" ;
			else 
				$s.="<td>&nbsp;</td>";			
				
		return $s;
	}

?>